<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="description" content="正则表达式"/>
    <meta name="keywords"
          content="nginx,freebsd,linux,mysql,shell,apache,centos,redhat,rhel,windows,sql,系统运维,IT运维,开源系统,开源系统运维"/>
    <title> 正则表达式 </title>

</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="menu">
    <ul>
		<span style="FONT-SIZE: 10.5pt">

			<li><a href="index.htm" hidefocus="true" id="mn_list">linux入门教程</a></li>

		</span>
    </ul>
</div>
<p style="text-align: center;"><span style="font-family:'宋体';font-size:15pt;"><b>第十三章</b></span><span
        style="font-family:'Calibri';font-size:15pt;"><b> </b></span><span style="font-family:'宋体';font-size:15pt;"><b>正则表达式</b></span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>	</b></span><span
        style="font-family:'宋体';font-size:10.5pt;">这部分内容可以说是学习</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本之前必学的内容</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">如果你这部分内容学的越好，那么你的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本编写能力就会越强</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以不要嫌这部分内容啰嗦，也不要怕麻烦，要用心学习</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">一定要多加练习，练习多了就能熟练掌握了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>


<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">在计算机科学中，正则表达式是这样解释的：它是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">在很多文本编辑器或其他工具里，正则表达式通常被用来检索和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">/</span><span style="font-family:'宋体';font-size:10.5pt;">或替换那些符合某个模式的文本内容</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">许多程序设计语言都支持利用正则表达式进行字符串操作</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">对于系统管理员来讲，正则表达式贯穿在我们的日常运维工作中，无论是查找某个文档，抑或查询某个日志文件分析其内容，都会用到正则表达式</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">其实正则表达式，只是一种思想，一种表示方法</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">只要我们使用的工具支持表示这种思想那么这个工具就可以处理正则表达式的字符串</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">常用的工具有</span><span style="font-family:'Calibri';font-size:10.5pt;">grep, sed, awk </span><span
        style="font-family:'宋体';font-size:10.5pt;">等，下面笔者就介绍一下这三种工具的使用方法</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">【</span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>grep / egrep</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;">】</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">笔者在前面的内容中多次提到并用到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">命令，可见它的重要性</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以好好学习一下这个重要的命令吧</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">你要知道的是</span><span style="font-family:'Calibri';font-size:10.5pt;">grep</span><span
        style="font-family:'宋体';font-size:10.5pt;">连同下面讲的</span><span style="font-family:'Calibri';font-size:10.5pt;">sed, awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">都是针对文本的行才操作的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">语法：</span><span style="font-family:'Calibri';font-size:10.5pt;"> grep  [-cinvABC]  ‘word’  filename </span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-c </span><span style="font-family:'宋体';font-size:10.5pt;">：打印符合要求的行数</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-i </span><span style="font-family:'宋体';font-size:10.5pt;">：忽略大小写</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-n </span><span style="font-family:'宋体';font-size:10.5pt;">：在输出符合要求的行的同时连同行号一起输出</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-v </span><span style="font-family:'宋体';font-size:10.5pt;">：打印不符合要求的行</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-A </span><span style="font-family:'宋体';font-size:10.5pt;">：后跟一个数字（有无空格都可以），例如</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> –A2</span><span style="font-family:'宋体';font-size:10.5pt;">则表示打印符合要求的行以及下面两行</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-B </span><span style="font-family:'宋体';font-size:10.5pt;">：后跟一个数字，例如</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> –B2 </span><span style="font-family:'宋体';font-size:10.5pt;">则表示打印符合要求的行以及上面两行</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-C </span><span style="font-family:'宋体';font-size:10.5pt;">：后跟一个数字，例如</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> –C2 </span><span style="font-family:'宋体';font-size:10.5pt;">则表示打印符合要求的行以及上下各两行</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_1.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">以下，笔者举几个小例子帮助你好好掌握这个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">工具的用法</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>a. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>过滤出带有某个关键词的行并输出行号</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_7.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>b. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>过滤不带有某个关键词的行，并输出行号</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_8.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>c. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>过滤出所有包含数字的行</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_9.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">在前面也提到过这个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”[ ]”</span><span style="font-family:'宋体';font-size:10.5pt;">的应用，如果是数字的话就用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[0-9]</span><span style="font-family:'宋体';font-size:10.5pt;">这样的形式，当然有时候也可以用这样的形式</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[15]</span><span
        style="font-family:'宋体';font-size:10.5pt;">即只含有</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span
        style="font-family:'宋体';font-size:10.5pt;">或者</span><span
        style="font-family:'Calibri';font-size:10.5pt;">5</span><span style="font-family:'宋体';font-size:10.5pt;">，注意，它不会认为是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">15。</span><span style="font-family:'宋体';font-size:10.5pt;">如果要过滤出数字以及大小写字母则要这样写</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[0-9a-zA-Z]。</span><span
        style="font-family:'宋体';font-size:10.5pt;">另外</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[ ]</span><span style="font-family:'宋体';font-size:10.5pt;">还有一种形式，就是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[^</span><span
        style="font-family:'宋体';font-size:10.5pt;">字符</span><span
        style="font-family:'Calibri';font-size:10.5pt;">] </span><span
        style="font-family:'宋体';font-size:10.5pt;">表示除</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[ ]</span><span style="font-family:'宋体';font-size:10.5pt;">内的字符之外的字符</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_10.png">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>d. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>过滤出文档中以某个字符开头或者以某个字符结尾的行</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_11.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">在正则表达式中，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”^”</span><span style="font-family:'宋体';font-size:10.5pt;">表示行的开始，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”$”</span><span style="font-family:'宋体';font-size:10.5pt;">表示行的结尾，那么空行则表示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”^$”,</span><span style="font-family:'宋体';font-size:10.5pt;">如果你只想筛选出非空行，则可以使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> “grep  -v  ‘^$’  filename”</span><span
        style="font-family:'宋体';font-size:10.5pt;">得到你想要的结果</span><span style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">现在想一下，如何打印出不以英文字母开头的行呢？</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_21.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>e. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>过滤任意一个字符与重复字符</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_22.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">“.”</span><span style="font-family:'宋体';font-size:10.5pt;">表示任意一个字符，上例中，就是把符合</span><span
        style="font-family:'Calibri';font-size:10.5pt;">r</span><span
        style="font-family:'宋体';font-size:10.5pt;">与</span><span
        style="font-family:'Calibri';font-size:10.5pt;">o</span><span style="font-family:'宋体';font-size:10.5pt;">之间有两个任意字符的行过滤出来</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">“*”</span><span style="font-family:'宋体';font-size:10.5pt;">表示零个或多个前面的字符</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_23.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">‘ooo*’ </span><span style="font-family:'宋体';font-size:10.5pt;">表示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">oo, ooo, oooo … </span><span
        style="font-family:'宋体';font-size:10.5pt;">或者更多的</span><span style="font-family:'Calibri';font-size:10.5pt;">’o’。</span><span
        style="font-family:'宋体';font-size:10.5pt;">现在你是否想到了</span><span style="font-family:'Calibri';font-size:10.5pt;">’.*’ </span><span
        style="font-family:'宋体';font-size:10.5pt;">这个组合表示什么意义？</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_24.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">‘.*’</span><span style="font-family:'宋体';font-size:10.5pt;">表示零个或多个任意字符，空行也包含在内</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>f. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>指定要过滤字符出现的次数</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_25.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">这里用到了</span><span style="font-family:'Calibri';font-size:10.5pt;">{ }</span><span
        style="font-family:'宋体';font-size:10.5pt;">，其内部为数字，表示前面的字符要重复的次数</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">上例中表示包含有两个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">o </span><span
        style="font-family:'宋体';font-size:10.5pt;">即</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’oo’</span><span
        style="font-family:'宋体';font-size:10.5pt;">的行</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">注意，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">{ }</span><span style="font-family:'宋体';font-size:10.5pt;">左右都需要加上脱意字符</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’\’。</span><span style="font-family:'宋体';font-size:10.5pt;">另外，使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">{ }</span><span style="font-family:'宋体';font-size:10.5pt;">我们还可以表示一个范围的，具体格式是</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> ‘\{n1,n2\}’</span><span
        style="font-family:'宋体';font-size:10.5pt;">其中</span><span style="font-family:'Calibri';font-size:10.5pt;">n1&lt;n2</span><span
        style="font-family:'宋体';font-size:10.5pt;">，表示重复</span><span
        style="font-family:'Calibri';font-size:10.5pt;">n1</span><span
        style="font-family:'宋体';font-size:10.5pt;">到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">n2</span><span style="font-family:'宋体';font-size:10.5pt;">次前面的字符，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">n2</span><span style="font-family:'宋体';font-size:10.5pt;">还可以为空，则表示大于等于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">n1</span><span
        style="font-family:'宋体';font-size:10.5pt;">次</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">上面部分讲的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">，另外笔者常常用到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">egrep</span><span style="font-family:'宋体';font-size:10.5pt;">这个工具，简单点讲，后者是前者的扩展版本，我们可以用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">egrep</span><span
        style="font-family:'宋体';font-size:10.5pt;">完成</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">不能完成的工作，当然了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span
        style="font-family:'宋体';font-size:10.5pt;">能完成的</span><span style="font-family:'Calibri';font-size:10.5pt;">egrep</span><span
        style="font-family:'宋体';font-size:10.5pt;">完全可以完成</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">如果你嫌麻烦，</span><span style="font-family:'Calibri';font-size:10.5pt;">egrep</span><span
        style="font-family:'宋体';font-size:10.5pt;">了解一下即可，因为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">的功能已经足够可以胜任你的日常工作了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">下面笔者介绍</span><span style="font-family:'Calibri';font-size:10.5pt;">egrep</span><span
        style="font-family:'宋体';font-size:10.5pt;">不用于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">的几个用法</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">为了试验方便，笔者把</span><span
        style="font-family:'Calibri';font-size:10.5pt;">test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">编辑成如下内容：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">rot:x:0:0:/rot:/bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">operator:x:11:0:operator:/root:/sbin/nologin</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">operator:x:11:0:operator:/rooot:/sbin/nologin</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">roooot:x:0:0:/rooooot:/bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">1111111111111111111111111111111</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>a. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>筛选一个或一个以上前面的字符</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_26.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep </span><span style="font-family:'宋体';font-size:10.5pt;">不同的是，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">egrep</span><span style="font-family:'宋体';font-size:10.5pt;">这里是使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’+’</span><span
        style="font-family:'宋体';font-size:10.5pt;">的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>b. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>筛选零个或一个前面的字符</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_27.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>c. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>筛选字符串</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>1</b></span><span style="font-family:'宋体';font-size:10.5pt;"><b>或者字符串</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>2</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_28.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">中间有一个</span><span style="font-family:'Calibri';font-size:10.5pt;">’|’</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示或者的意思，笔者用这个用的很多，所以这个你最好记一下</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>d.  egrep</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>中</b></span><span style="font-family:'Calibri';font-size:10.5pt;"><b>’( )’</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>的应用</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_29.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’( )’</span><span style="font-family:'宋体';font-size:10.5pt;">表示一个整体，例如</span><span
        style="font-family:'Calibri';font-size:10.5pt;">(oo)+</span><span
        style="font-family:'宋体';font-size:10.5pt;">就表示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span
        style="font-family:'宋体';font-size:10.5pt;">个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’oo’</span><span
        style="font-family:'宋体';font-size:10.5pt;">或者多个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’oo’</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_44.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">【</span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>sed </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>工具的使用</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;">】</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	grep </span><span
        style="font-family:'宋体';font-size:10.5pt;">工具的功能其实还不够强大，其实说白了，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">实现的只是查找功能，而它却不能实现把查找的内容替换掉</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">以前用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">vim</span><span style="font-family:'宋体';font-size:10.5pt;">的时候，可以查找也可以替换，但是只局限于在文本内部来操作，而不能输出到屏幕上</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。sed</span><span style="font-family:'宋体';font-size:10.5pt;">工具以及下面要讲的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">工具就能实现把替换的文本输出到屏幕上的功能了，而且还有其他更丰富的功能</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。sed</span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">都是流式编辑器，是针对文档的行来操作的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>a. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>打印某行</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>   sed -n ‘n’p filename </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>单引号内的</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>n</b></span><span style="font-family:'宋体';font-size:10.5pt;"><b>是一个数字，表示第几行</b></span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_45.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>b. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>打印多行</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>  </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>打印整个文档用</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b> -n ‘1,$’p  </b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_46.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>c. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>打印包含某个字符串的行</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_47.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">上面</span><span
        style="font-family:'Calibri';font-size:10.5pt;">grep</span><span style="font-family:'宋体';font-size:10.5pt;">中使用的特殊字符，如</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’^’, ‘$’, ‘.’, ‘*’</span><span
        style="font-family:'宋体';font-size:10.5pt;">等同样也能在</span><span style="font-family:'Calibri';font-size:10.5pt;">sed</span><span
        style="font-family:'宋体';font-size:10.5pt;">中使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_48.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_49.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>d. -e </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>可以实现多个行为</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_50.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>e. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>删除某行或者多行</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_51.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">‘d’ </span><span style="font-family:'宋体';font-size:10.5pt;">这个字符就是删除的动作了，不仅可以删除指定的单行以及多行，而且还可以删除匹配某个字符的行，另外还可以删除从某一行一直到文档末行</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_52.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>f. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>替换字符或字符串</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_53.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">上例中的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’s’</span><span style="font-family:'宋体';font-size:10.5pt;">就是替换的命令，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’g’</span><span style="font-family:'宋体';font-size:10.5pt;">为本行中全局替换，如果不加</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’g’</span><span style="font-family:'宋体';font-size:10.5pt;">，只换该行中出现的第一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">除了可以使用</span><span style="font-family:'Calibri';font-size:10.5pt;">’/’</span><span
        style="font-family:'宋体';font-size:10.5pt;">外，还可以使用其他特殊字符例如</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’#’</span><span
        style="font-family:'宋体';font-size:10.5pt;">或者</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’@’</span><span
        style="font-family:'宋体';font-size:10.5pt;">都没有问题</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_54.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">现在思考一下，如何删除文档中的所有数字或者字母？</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_55.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">有意思吧，</span><span style="font-family:'Calibri';font-size:10.5pt;">[0-9]</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示任意的数字</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">这里你也可以写成</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[a-zA-Z]</span><span style="font-family:'宋体';font-size:10.5pt;">甚至</span><span
        style="font-family:'Calibri';font-size:10.5pt;">[0-9a-zA-Z]</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_56.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>g. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>调换两个字符串的位置</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_57.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">这个就需要解释一下了，上例中用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’()’</span><span style="font-family:'宋体';font-size:10.5pt;">把所想要替换的字符括起来成为一个整体，因为括号在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">中属于特殊符号，所以需要在前面加脱意字符</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’\’</span><span style="font-family:'宋体';font-size:10.5pt;">，替换时则写成</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’\1’, ‘\2’, ‘\3’ </span><span
        style="font-family:'宋体';font-size:10.5pt;">的形式</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">除了调换两个字符串的位置外，笔者还常常用到在某一行前或者后增加指定内容</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_78.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>h. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>直接修改文件的内容</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">sed -i ‘s/:/#/g’  test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">，这样就可以直接更改</span><span
        style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span style="font-family:'宋体';font-size:10.5pt;">文件中的内容了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">由于这个命令可以直接把文件修改，所以在修改前最好先复制一下文件以免改错</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">sed</span><span
        style="font-family:'宋体';font-size:10.5pt;">常用到的也就上面这些了，只要你多加练习就能熟悉它了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">为了能让你更加牢固的掌握</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">的应用，笔者留几个练习题给你，希望你能认真完成</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">1. </span><span
        style="font-family:'宋体';font-size:10.5pt;">把</span><span style="font-family:'Calibri';font-size:10.5pt;">/etc/passwd </span><span
        style="font-family:'宋体';font-size:10.5pt;">复制到</span><span style="font-family:'Calibri';font-size:10.5pt;">/root/test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">，用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">打印所有行；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">2. </span><span
        style="font-family:'宋体';font-size:10.5pt;">打印</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">3</span><span
        style="font-family:'宋体';font-size:10.5pt;">到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">10</span><span
        style="font-family:'宋体';font-size:10.5pt;">行；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">3. </span><span
        style="font-family:'宋体';font-size:10.5pt;">打印</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">中包含</span><span style="font-family:'Calibri';font-size:10.5pt;">’root’</span><span
        style="font-family:'宋体';font-size:10.5pt;">的行；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">4. </span><span
        style="font-family:'宋体';font-size:10.5pt;">删除</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">15</span><span style="font-family:'宋体';font-size:10.5pt;">行以及以后所有行；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">5. </span><span
        style="font-family:'宋体';font-size:10.5pt;">删除</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">中包含</span><span style="font-family:'Calibri';font-size:10.5pt;">’bash’</span><span
        style="font-family:'宋体';font-size:10.5pt;">的行；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">6. </span><span
        style="font-family:'宋体';font-size:10.5pt;">替换</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">中</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’root’</span><span
        style="font-family:'宋体';font-size:10.5pt;">为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’toor’</span><span
        style="font-family:'宋体';font-size:10.5pt;">；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">7. </span><span
        style="font-family:'宋体';font-size:10.5pt;">替换</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">中</span><span style="font-family:'Calibri';font-size:10.5pt;">’/sbin/nologin’</span><span
        style="font-family:'宋体';font-size:10.5pt;">为</span><span style="font-family:'Calibri';font-size:10.5pt;">’/bin/login’</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">8. </span><span
        style="font-family:'宋体';font-size:10.5pt;">删除</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">中</span><span
        style="font-family:'Calibri';font-size:10.5pt;">5</span><span
        style="font-family:'宋体';font-size:10.5pt;">到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">10</span><span style="font-family:'宋体';font-size:10.5pt;">行中所有的数字；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">9. </span><span
        style="font-family:'宋体';font-size:10.5pt;">删除</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">中所有特殊字符（除了数字以及大小写字母）；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">10. </span><span
        style="font-family:'宋体';font-size:10.5pt;">把</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">中第一个单词和最后一个单词调换位置；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">11. </span><span
        style="font-family:'宋体';font-size:10.5pt;">把</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">中出现的第一个数字和最后一个单词替换位置；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">12. </span><span
        style="font-family:'宋体';font-size:10.5pt;">把</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">中第一个数字移动到行末尾；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">13. </span><span
        style="font-family:'宋体';font-size:10.5pt;">在</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt  20</span><span
        style="font-family:'宋体';font-size:10.5pt;">行到末行最前面加</span><span style="font-family:'Calibri';font-size:10.5pt;">’aaa:’</span><span
        style="font-family:'宋体';font-size:10.5pt;">；</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">现在给出以上练习题的答案，你如果实在想不出如何操作，那你看看答案吧，请尽量多想一下</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">1.  /bin/cp /etc/passwd  /root/test.txt ;  sed -n '1,$'p test.txt</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">2.  sed -n '3,10'p test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">3.  sed -n '/root/'p test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">4.  sed '15,$'d  test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">5.  sed '/bash/'d test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">6.  sed 's/root/toor/g' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">7.  sed 's#sbin/nologin#bin/login#g' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">8.  sed '5,10s/[0-9]//g' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">9.  sed 's/[^0-9a-zA-Z]//g' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">10.  sed 's/\(^[a-zA-Z][a-zA-Z]*\)\([^a-zA-Z].*\)\([^a-zA-Z]\)\([a-zA-Z][a-zA-Z]*$\)/\4\2\3\1/' test.txt</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">11.  sed 's#\([^0-9][^0-9]*\)\([0-9][0-9]*\)\([^0-9].*\)\([^a-zA-Z]\)\([a-zA-Z][a-zA-Z]*$\)#\1\5\3\4\2#' test.txt</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">12.  sed 's#\([^0-9][^0-9]*\)\([0-9][0-9]*\)\([^0-9].*$\)#\1\3\2#' test.txt</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">13.  sed '20,$s/^.*$/aaa:&amp;/' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">【</span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>awk</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>工具的使用</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;">】</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">上面也提到了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">一样是流式编辑器，它也是针对文档中的行来操作的，一行一行的去执行</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">比</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">更加强大，它能做到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">能做到的，同样也能做到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span
        style="font-family:'宋体';font-size:10.5pt;">不能做到的</span><span style="font-family:'Calibri';font-size:10.5pt;">。awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">工具其实是很复杂的，有专门的书籍来介绍它的应用，但是笔者认为学那么复杂没有必要，只要能处理日常管理工作中的问题即可</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">何必让自己的脑袋装那么东西来为难自己？毕竟用的也不多，即使现在教会了你很多，你也学会了，如果很久不用肯定就忘记了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">鉴于此，笔者仅介绍比较常见的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">应用，如果你感兴趣的话，再去深入研究吧</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>a. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>截取文档中的某个段</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_79.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">解释一下，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-F </span><span style="font-family:'宋体';font-size:10.5pt;">选项的作用是指定分隔符，如果不加</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-F</span><span style="font-family:'宋体';font-size:10.5pt;">指定，则以空格或者</span><span
        style="font-family:'Calibri';font-size:10.5pt;">tab</span><span
        style="font-family:'宋体';font-size:10.5pt;">为分隔符</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_80.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">Print</span><span style="font-family:'宋体';font-size:10.5pt;">为打印的动作，用来打印出某个字段</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。$1</span><span style="font-family:'宋体';font-size:10.5pt;">为第一个字段，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$2</span><span style="font-family:'宋体';font-size:10.5pt;">为第二个字段，依次类推，有一个特殊的那就是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$0</span><span
        style="font-family:'宋体';font-size:10.5pt;">，它表示整行</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_81.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">注意</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">的格式，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-F</span><span style="font-family:'宋体';font-size:10.5pt;">后紧跟单引号，然后里面为分隔符，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">print</span><span style="font-family:'宋体';font-size:10.5pt;">的动作要用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’{ }’</span><span style="font-family:'宋体';font-size:10.5pt;">括起来，否则会报错</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。print</span><span style="font-family:'宋体';font-size:10.5pt;">还可以打印自定义的内容，但是自定义的内容要用双引号括起来</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_82.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>b. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>匹配字符或字符串</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_83.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">跟</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">很类似吧，不过还有比</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span style="font-family:'宋体';font-size:10.5pt;">更强大的匹配</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_84.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">可以让某个段去匹配，这里的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’~’</span><span style="font-family:'宋体';font-size:10.5pt;">就是匹配的意思，继续往下看</span><span
        style="font-family:'Calibri';font-size:10.5pt;"></span></p>
<p><img src="13_85.png"></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">还可以多次匹配，如上例中匹配完</span><span
        style="font-family:'Calibri';font-size:10.5pt;">root</span><span
        style="font-family:'宋体';font-size:10.5pt;">，再匹配</span><span
        style="font-family:'Calibri';font-size:10.5pt;">test</span><span style="font-family:'宋体';font-size:10.5pt;">，它还可以只打印所匹配的段</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_86.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">不过这样没有啥意义，笔者只是为了说明</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">确实比</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span
        style="font-family:'宋体';font-size:10.5pt;">强大</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>d. </b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>条件操作符</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_87.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">中是可以用逻辑符号判断的，比如</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’==’</span><span style="font-family:'宋体';font-size:10.5pt;">就是等于，也可以理解为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">“</span><span
        style="font-family:'宋体';font-size:10.5pt;">精确匹配</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”。</span><span
        style="font-family:'宋体';font-size:10.5pt;">另外也有</span><span style="font-family:'Calibri';font-size:10.5pt;">’&gt;’, ‘&gt;=’, ‘&lt;’, ‘&lt;=’, ‘!=’ </span><span
        style="font-family:'宋体';font-size:10.5pt;">等等，值得注意的是，即使</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$3</span><span
        style="font-family:'宋体';font-size:10.5pt;">为数字，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">也不会把它当数字看待，它会认为是一个字符</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以不要妄图去拿</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$3</span><span style="font-family:'宋体';font-size:10.5pt;">当数字去和数字做比较</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_88.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">这样是得不到我们想要的效果的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">这里只是字符与字符之间的比较，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’6’</span><span
        style="font-family:'宋体';font-size:10.5pt;">是</span><span style="font-family:'Calibri';font-size:10.5pt;">&gt;’500’</span><span
        style="font-family:'宋体';font-size:10.5pt;">的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_89.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">上例中用的是</span><span style="font-family:'Calibri';font-size:10.5pt;">’!=’ </span><span
        style="font-family:'宋体';font-size:10.5pt;">即不匹配</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_90.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">另外还可以使用</span><span style="font-family:'Calibri';font-size:10.5pt;">”&amp;&amp;” </span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> “||”</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">“</span><span
        style="font-family:'宋体';font-size:10.5pt;">并且</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”</span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">“</span><span
        style="font-family:'宋体';font-size:10.5pt;">或者</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”</span><span
        style="font-family:'宋体';font-size:10.5pt;">的意思</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_91.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">也可以是或者的关系</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_92.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>d. awk</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>的内置变量</b></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">常用的变量有：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">NF </span><span style="font-family:'宋体';font-size:10.5pt;">：用分隔符分隔后一共有多少段；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">NR </span><span
        style="font-family:'宋体';font-size:10.5pt;">：行数</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_93.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">上例中，打印总共的段数以及最后一段的值</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_94.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">可以使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">NR</span><span style="font-family:'宋体';font-size:10.5pt;">作为条件，来打印出指定的行</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><img src="13_95.png"></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>e. awk</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>中的数学运算</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_96.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">比较强的地方，还在于能把某个段改成指定的字符串，下面还有更强的呢！</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_97.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">当然还可以计算某个段的总和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_101.png">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="13_102.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">注意这里</span><span style="font-family:'Calibri';font-size:10.5pt;">’( )’</span><span
        style="font-family:'宋体';font-size:10.5pt;">的使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>	</b></span><span
        style="font-family:'宋体';font-size:10.5pt;">基本上，正则表达的内容就这些了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">但是笔者要提醒你一下，笔者介绍的这些仅仅是最基本的东西，并没有提啊深入的去讲</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk</span><span style="font-family:'宋体';font-size:10.5pt;">，但是完全可以满足日常工作的需要，有时候也许你会碰到比较复杂的需求，如果真遇到了就去请教一下</span><span
        style="font-family:'Calibri';font-size:10.5pt;">google</span><span
        style="font-family:'宋体';font-size:10.5pt;">吧</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">下面出几道关于</span><span style="font-family:'Calibri';font-size:10.5pt;">awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">的练习题，希望你要认真完成</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">1. </span><span
        style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">awk </span><span
        style="font-family:'宋体';font-size:10.5pt;">打印整个</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt </span><span
        style="font-family:'宋体';font-size:10.5pt;">（以下操作都是用</span><span style="font-family:'Calibri';font-size:10.5pt;">awk</span><span
        style="font-family:'宋体';font-size:10.5pt;">工具实现，针对</span><span style="font-family:'Calibri';font-size:10.5pt;">test.txt</span><span
        style="font-family:'宋体';font-size:10.5pt;">）；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">2. </span><span style="font-family:'宋体';font-size:10.5pt;">查找所有包含</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’bash’</span><span style="font-family:'宋体';font-size:10.5pt;">的行；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">3. </span><span
        style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’:’</span><span style="font-family:'宋体';font-size:10.5pt;">作为分隔符，查找第三段等于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">0</span><span
        style="font-family:'宋体';font-size:10.5pt;">的行；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">4. </span><span
        style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’:’</span><span style="font-family:'宋体';font-size:10.5pt;">作为分隔符，查找第一段为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’root’</span><span style="font-family:'宋体';font-size:10.5pt;">的行，并把该段的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’root’</span><span
        style="font-family:'宋体';font-size:10.5pt;">换成</span><span style="font-family:'Calibri';font-size:10.5pt;">’toor’(</span><span
        style="font-family:'宋体';font-size:10.5pt;">可以连同</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sed</span><span
        style="font-family:'宋体';font-size:10.5pt;">一起使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">)</span><span
        style="font-family:'宋体';font-size:10.5pt;">；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">5. </span><span
        style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’:’</span><span style="font-family:'宋体';font-size:10.5pt;">作为分隔符，打印最后一段；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">6. </span><span style="font-family:'宋体';font-size:10.5pt;">打印行数大于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">20</span><span
        style="font-family:'宋体';font-size:10.5pt;">的所有行；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">7. </span><span
        style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’:’</span><span style="font-family:'宋体';font-size:10.5pt;">作为分隔符，打印所有第三段小于第四段的行；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">8. </span><span
        style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’:’</span><span style="font-family:'宋体';font-size:10.5pt;">作为分隔符，打印第一段以及最后一段，并且中间用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’@’</span><span
        style="font-family:'宋体';font-size:10.5pt;">连接</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> </span><span style="font-family:'宋体';font-size:10.5pt;">（例如，第一行应该是这样的形式</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> “root@/bin/bash”</span><span
        style="font-family:'宋体';font-size:10.5pt;">；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">9. </span><span
        style="font-family:'宋体';font-size:10.5pt;">用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’:’</span><span style="font-family:'宋体';font-size:10.5pt;">作为分隔符，把整个文档的第四段相加，求和；</span>
</p>
<p><span style="font-family:'宋体';font-size:10.5pt;">下面给出答案：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">1. awk '{print $0}' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">2. awk '/bash/' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">3. awk -F':' '$3=="0"' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">4. awk -F':' '$1=="root"' test.txt |sed 's/root/toor/'</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">5. awk -F':' '{print $NF}' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">6. awk -F':' 'NR&gt;20' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">7. awk -F':' '$3&lt;$4' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">8. awk -F':' '{print $1"@"$NF}' test.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">9. awk -F':' '{(sum+=$4)}; END {print sum}' test.txt</span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>

<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">上一页 <a
        href="12.htm">学习shell脚本之前的基础知识</a></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">下一页 <a
        href="14.htm">SHELL 脚本</a></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;"><a
        href="index.htm">回到主目录</a>&nbsp;</span></p>

</body>
</html>